package com.github.cosycode.imgtool.rule;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.function.Predicate;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2022/8/7
 * </p>
 *
 * @author pengfchen
 * @since 1.0
 **/
@Getter
@ToString
public class CheckConfig {
    boolean absolutePathEnable;
    boolean relatePathEnable;
    boolean rootPathEnable;
    /**
     * 项目根路径: 若 md 文件中的图片链接是项目根路径时会用到该值.
     */
    String rootPath;
    String parentPath;
    boolean webPathEnable;
    boolean connectWeb;

    @Setter
    @Getter
    Predicate<String> uriFilter;

    public static CheckConfig geneConfig(boolean absolute, String rootPath, boolean relate) {
        CheckConfig config = new CheckConfig();
        config.setAbsolutePathEnable(absolute);
        config.setRootPathEnable(rootPath);
        config.setRelatePathEnable(relate);
        return config;
    }

    public void setAbsolutePathEnable(boolean absolutePathEnable) {
        this.absolutePathEnable = absolutePathEnable;
    }

    public void setRelatePathEnable(String parentPath) {
        if (StringUtils.isEmpty(parentPath)) {
            this.relatePathEnable = false;
            this.parentPath = null;
        } else {
            this.relatePathEnable = true;
            this.parentPath = parentPath;
        }
    }

    public void setRelatePathEnable(boolean flag) {
        this.relatePathEnable = flag;
    }

    public void setRootPathEnable(String rootPath) {
        if (StringUtils.isEmpty(rootPath)) {
            this.rootPathEnable = false;
            this.rootPath = null;
        } else {
            this.rootPathEnable = true;
            this.rootPath = rootPath;
        }
    }

    /**
     * @param webPathEnable 检查 WEB_URL 的标记
     * @param connectWeb    (首先 webPathEnable 为 true 才有意义), 当前为
     */
    public void setWebPathEnable(boolean webPathEnable, boolean connectWeb) {
        this.webPathEnable = webPathEnable;
        this.connectWeb = connectWeb;
    }

}
